home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-04 | 89.3 KB | 3,217 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UTEView.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UTEVIEW__
- #include "UTEView.h"
- #endif
-
- // MacApp
-
- // #ifndef __UAPPLICATION__
- // #include "UApplication.h"
- // #endif
-
- #ifndef __UCLIPBOARDMGR__
- #include "UClipboardMgr.h"
- #endif
-
- #ifndef __UDEBUG__
- #include "UDebug.h"
- #endif
-
- #ifndef __UDISPATCHER__
- #include "UDispatcher.h"
- #endif
-
- #ifndef __UDOCUMENT__
- #include "UDocument.h"
- #endif
-
- #if qDrag
- #ifndef __UDRAGDROP__
- #include "UDragDrop.h"
- #endif
-
- #endif
-
- #ifndef __UDRAGDROPBEHAVIOR__
- #include "UDragDropBehavior.h"
- #endif
-
- #ifndef __UERRORMGR__
- #include "UErrorMgr.h"
- #endif
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
- #ifndef __UFILE__
- #include "UFile.h"
- #endif
-
- #ifndef __UGEOMETRY__
- #include "UGeometry.h"
- #endif
-
- #ifndef __ULIST__
- #include "UList.h"
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UMENUMGR__
- #include "UMenuMgr.h"
- #endif
-
- #ifndef __UPATCH__
- #include "UPatch.h"
- #endif
-
- #ifndef __UPRINTHANDLER__
- #include "UPrintHandler.h"
- #endif
-
- #ifndef __USCROLLER__
- #include "UScroller.h"
- #endif
-
- #ifndef __USTREAM__
- #include "UStream.h"
- #endif
-
- #ifndef __UTECOMMANDS__
- #include "UTECommands.h"
- #endif
-
- #ifndef __UWINDOW__
- #include "UWindow.h"
- #endif
-
- // Toolbox
-
- #ifndef __ALIASES__
- #include <Aliases.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __EDITIONS__
- #include <Editions.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- #ifndef __GESTALT__
- #include <Gestalt.h>
- #endif
-
- #ifndef __LOWMEM__
- #include <LowMem.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- #ifndef __SCRAP__
- #include <Scrap.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- // ANSI
-
- #ifndef __LIMITS__
- #include <limits.h>
- #endif
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
-
- //========================================================================================
- // static data members
- //========================================================================================
-
- TEClickLoopUPP TTEView::fgDefClickLoopProc; // Standard TextEdit click loop routine
- TTEView* TTEView::fgCurrTEView;
- TEClickLoopUPP TTEView::fgClickLoopProc;
- #if qDebug
- Boolean TTEView::fgTEIntenseDebugging; // (has external) For the benefit of ClickLoopForTTEView only
- #endif
-
- //----------------------------------------------------------------------------------------
- // InitUTEView:
- //----------------------------------------------------------------------------------------
- #pragma segment TEInit
-
- void InitUTEView()
- {
- if (!gUTEViewInitialized)
- {
- gUTEViewInitialized = TRUE;
- #if qTemplateViews
- {
- // So we can create TTEView objects from view resources
- MA_REGISTER_SIGNATURE(TTEView, kStdTEView);
- }
- #endif
-
- if (!TTEView::fgClickLoopProc)
- FailNIL(TTEView::fgClickLoopProc = NewTEClickLoopProc(TTEView::ClickLoopForTTEView));
- }
- }
-
- //========================================================================================
- // CLASS TTEView
- //========================================================================================
- #undef Inherited
- #define Inherited TView
-
- #pragma segment TEOpen
- MA_DEFINE_CLASS_M1(TTEView,
- Inherited);
-
- //----------------------------------------------------------------------------------------
- // TTEView constructor
- //----------------------------------------------------------------------------------------
- #pragma segment TEOpen
-
- TTEView::TTEView()
- {
- fWantsToBeTarget = TRUE;
- fCursorID = iBeamCursor;
-
- fTextStyle = gSystemStyle;
- fInset = gZeroVRect;
- fTypingCommand = NULL;
- fHTE = NULL;
- fText = NULL;
- fSavedTEHandle = NULL;
- fLastHeight = 0;
- fLastWidth = 0;
- fMinAhead = kMinAhead;
- fLastPageBreak = 0;
- fKeyCommandNumber = cTyping;
- // fControlChars = [chLeft, chRight, chUp, chDown, chBackspace, chReturn];
- fControlChars = macroAsSetElem(chLeft) | macroAsSetElem(chRight) | macroAsSetElem(chUp) | macroAsSetElem(chDown) | macroAsSetElem(chBackspace) | macroAsSetElem(chReturn);
- fMaxChars = kUnlimited;
- fTextStyleRsrcID = kNoResource;
- fJustification = teFlushDefault;
- fLastLine = 0;
- fSelAnchor = 0;
- fUpDownH = 0;
- fPreferOutline = kDontPreferOutline;
- fAcceptsChanges = TRUE; // Stuff to false if you don't want to
- // allow Cut, Paste, or Typing
- fStyleType = kWithStyle;
- fAutoWrap = TRUE;
- fFreeText = TRUE;
- fSpecsChanged = FALSE;
- fUpDown = FALSE;
- #if qDrag
- fHiliteRgn = NULL;
- fLastCaretTime = 0L;
- fLastCaretOffset = 0;
- fCaretIsShown = FALSE;
- #endif // qDrag
-
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ITEView:
- //----------------------------------------------------------------------------------------
- #pragma segment TEOpen
-
- void TTEView::ITEView(TDocument* itsDocument,
- TView* itsSuperView,
- const VPoint& itsLocation,
- const VPoint& itsSize,
- SizeDeterminer itsHDeterminer,
- SizeDeterminer itsVDeterminer,
- const VRect& itsInset,
- const TextStyle& itsTextStyle,
- short itsJustification,
- Boolean itsStyleType,
- Boolean itsAutoWrap)
-
- {
- IView(itsDocument, itsSuperView, itsLocation, itsSize, itsHDeterminer, itsVDeterminer);
-
- #if qDebug
- if (!gUTEViewInitialized)
- {
- ProgramBreak("InitUTEView must be called before creating a TE View.");
- Failure(noErr, 0);
- }
- #endif
-
- fInset = itsInset;
- fTextStyle = itsTextStyle;
- fJustification = itsJustification;
- fStyleType = itsStyleType;
- fAutoWrap = itsAutoWrap;
-
- FailInfo fi;
- Try(fi)
- {
- MakeTERecord();
-
- fSelAnchor = (*fHTE)->selStart;
- TESetClickLoop(fgClickLoopProc, fHTE);
- fText = (*fHTE)->hText;
-
- SetIdleFreq(0); // Idle ASAP
-
- TEFeatureFlag(teFOutlineHilite, teBitSet, fHTE);
-
- AddAdorner(gSelectionAdorner, kDrawView, FALSE);// Wants DoHighlightSelection called
- fi.Success();
- }
- else // Recover
- {
- Free();
- fi.ReSignal();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::Clone:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- TObject* TTEView::Clone() // override
- {
- MAVolatileInit(TEStyleHandle, theStyles, NULL);
- MAVolatileInit(STHandle, theElements, NULL);
- MAVolatileInit(LHHandle, theLineHeights, NULL);
- MAVolatileInit(NullStHandle, theNullStyles, NULL);
- MAVolatileInit(Handle, theText, NULL);
-
- MAVolatileInit(TTEView * , aClonedTTEView, (TTEView *)(Inherited::Clone()));
- MAVolatileInit(SignedByte, wasState, 0);
-
- aClonedTTEView->fHTE = NULL;
- aClonedTTEView->fSavedTEHandle = NULL;
- aClonedTTEView->fText = NULL;
- aClonedTTEView->fTypingCommand = NULL;
-
- FailInfo fi;
- Try(fi)
- {
- NullStHandle oldNullStyles = NULL;
- StScrpHandle oldNullScrap = NULL;
- LHHandle oldLineHeights = NULL;
- STHandle oldElements = NULL;
- TEStyleHandle oldStyles = NULL;
- STHandle inputElements = NULL;
-
- if (fHTE)
- {
- GrafPtr oldPort;
- TextStyle aTextStyle;
-
- CWhileOutlinePreferred setOP(fPreferOutline);
-
- GetPort(&oldPort);
- SetPortWindowPort(gWorkPort);
- aTextStyle = fTextStyle;
- SetPortTextStyle(aTextStyle);
-
- wasState = LockHandle((Handle)(fHTE));
- {
- CRect destRect((*fHTE)->destRect);
- CRect viewRect((*fHTE)->viewRect);
- if (fStyleType == kWithStyle)
- aClonedTTEView->fHTE = TEStyleNew(&destRect, &viewRect);
- else
- aClonedTTEView->fHTE = TENew(&destRect, &viewRect);
- }
-
- SetPort(oldPort);
-
- FailNIL(aClonedTTEView->fHTE);
-
- // Copy some fields from the original TERecord into the cloned TERecord
- (*(aClonedTTEView->fHTE))->just = (*fHTE)->just;
- (*(aClonedTTEView->fHTE))->crOnly = (*fHTE)->crOnly;
- TESetWordBreak((*fHTE)->wordBreak, aClonedTTEView->fHTE);
- TESetClickLoop((*fHTE)->clickLoop, aClonedTTEView->fHTE);
-
- // These internal fields need to be cloned too
- (*(aClonedTTEView->fHTE))->selRect = (*fHTE)->selRect;
- (*(aClonedTTEView->fHTE))->selPoint = (*fHTE)->selPoint;
-
- // Clone the styles record since clone or stuffstyles doesn't do it for us
- if (fStyleType == kWithStyle)
- {
- TEStyleHandle inputStyles;
-
- ExtractStyles(inputStyles, inputElements);
- oldStyles = TEGetStyleHandle(aClonedTTEView->fHTE);
-
- // Remember pointers to the memory blocks allocated by TEStyleNew
- oldElements = (*oldStyles)->styleTab;
- oldLineHeights = (*oldStyles)->lhTab;
- oldNullStyles = (*oldStyles)->nullStyle;
-
- if (oldNullStyles)
- oldNullScrap = (*oldNullStyles)->nullScrap;
- else
- oldNullScrap = NULL;
-
- theStyles = inputStyles;
- PermHandToHand((Handle &)theStyles);
-
- theElements = inputElements;
- PermHandToHand((Handle &)theElements);
-
- theLineHeights = (*inputStyles)->lhTab;
- PermHandToHand((Handle &)theLineHeights);
-
- theNullStyles = (*inputStyles)->nullStyle;
- if (theNullStyles)
- {
- PermHandToHand((Handle &)theNullStyles);
- if (oldNullScrap)
- {
- StScrpHandle theNullScrap = oldNullScrap;
- PermHandToHand((Handle &)theNullScrap);
- (*theNullStyles)->nullScrap = theNullScrap;
- }
- }
-
- (*theStyles)->nullStyle = theNullStyles;// Replace NULL style handle
- (*theStyles)->lhTab = theLineHeights;// Replace line heights table handle
- (*theStyles)->styleTab = theElements;// Replace STElements handle
-
- (*theStyles)->teRefCon = (*oldStyles)->teRefCon;// clone the refCon
-
- // Now that the cloned elements have been added to the
- // TE Record, delete the old versions (allocated by
- // TEStyleNew) and clear out the local variables that
- // point to the cloned items, so that they failure
- // handler will not try to free the same item twice.
- theElements = NULL;
- theLineHeights = NULL;
- theNullStyles = NULL;
- oldElements = (STHandle)(DisposeIfHandle((Handle)oldElements));
- oldLineHeights = (LHHandle)(DisposeIfHandle((Handle)oldLineHeights));
- oldNullStyles = (NullStHandle)(DisposeIfHandle((Handle)oldNullStyles));
- oldNullScrap = (StScrpHandle)(DisposeIfHandle((Handle)oldNullScrap));
-
- // NOTE!! TESetStyleHandle will dispose of oldStyles for us, but
- // not any of the handles contained INSIDE of oldStyles (argh!)
- // TextEdit is an Evil Dog.
- TESetStyleHandle(theStyles, aClonedTTEView->fHTE);
- theStyles = NULL;
- }
-
- aClonedTTEView->fSavedTEHandle = (*aClonedTTEView->fHTE)->hText;// Save existing handle
- theText = fText;
- PermHandToHand((Handle &)theText);
- (*aClonedTTEView->fHTE)->hText = theText;// Install new handle
- aClonedTTEView->fText = theText; // Make a local copy, too
- theText = NULL;
-
- // Don't forget to update the length field of the new TE Record
- (*aClonedTTEView->fHTE)->teLength = (*fHTE)->teLength;
-
- // Copy the insertion point of the old TE record
- aClonedTTEView->SetSelection((*fHTE)->selStart, (*fHTE)->selEnd, kDontRedraw);
-
- HSetState((Handle)(fHTE), wasState);
- }
- fi.Success();
- }
- else // Recover
- {
- // Usually, aClonedTTEView->Free will delete these
- // elements for us, but we still have to worry about the
- // case where PermHandToHand fails before the structures
- // we are cloning are assigned to the TE Record. This
- // is why these local variables are set to NULL as soon
- // as they are added to the TE Record.
- theElements = (STHandle)(DisposeIfHandle((Handle)theElements));
- theLineHeights = (LHHandle)(DisposeIfHandle((Handle)theLineHeights));
- theNullStyles = (NullStHandle)(DisposeIfHandle((Handle)theNullStyles));
- theText = DisposeIfHandle(theText);
- theStyles = (TEStyleHandle)(DisposeIfHandle((Handle)theStyles));
-
- aClonedTTEView->Free();
-
- // We shouldn't leave this handle locked if we fail to clone it
- if (fHTE)
- HUnlock((Handle)(fHTE));
-
- fi.ReSignal();
- }
-
- aClonedTTEView->RecalcText();
-
- return aClonedTTEView;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment TEClose
-
- TTEView::~TTEView()
- {
- if (fgCurrTEView == this)
- fgCurrTEView = NULL;
-
- if (fHTE)
- {
- if (fSavedTEHandle)
- {
- // Worry about fText separately. Put back the handle which TE allocated.
- (*fHTE)->hText = fSavedTEHandle;
-
- // This is here because it only makes sense if fSavedTEHandle is not NULL.
- (*fHTE)->teLength = (short)GetHandleSize((*fHTE)->hText);//!!! Note Size->short cast
-
- if (fFreeText)
- fText = DisposeIfHandle(fText);
- else
- fText = NULL; // Always drop my reference
- }
- TEDispose(fHTE);
- fHTE = NULL;
-
- fSavedTEHandle = NULL;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetStandardSignature:
- //----------------------------------------------------------------------------------------
- #pragma segment TEWriteResource
-
- IDType TTEView::GetStandardSignature() // override
- {
- return kStdTEView;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ReadFields:
- //----------------------------------------------------------------------------------------
- #pragma segment TEReadResource
-
- void TTEView::ReadFields(TStream* aStream) // override
- {
- VRect vr;
-
- Inherited::ReadFields(aStream);
-
- #if qDebug
- if (!gUTEViewInitialized)
- {
- ProgramBreak("InitUTEView must be called before creating a TE View.");
- Failure(noErr, 0);
- }
- #endif
-
- FailInfo fi;
- Try(fi)
- {
- fStyleType = aStream->ReadBoolean();
- fAutoWrap = aStream->ReadBoolean();
- fAcceptsChanges = aStream->ReadBoolean();
- fFreeText = aStream->ReadBoolean();
- fKeyCommandNumber = aStream->ReadLong();
- fMaxChars = aStream->ReadInteger();
- aStream->ReadVRect(vr);
- fInset = vr;
- fJustification = aStream->ReadInteger();
- fTextStyleRsrcID = aStream->ReadInteger();
- if (fTextStyleRsrcID != kNoResource)
- {
- TextStyle itsTextStyle;
- MAGetTextStyle(fTextStyleRsrcID, itsTextStyle);
- fTextStyle = itsTextStyle;
- }
-
- fPreferOutline = aStream->ReadBoolean();
-
- MakeTERecord();
- fi.Success();
- }
- else // Recover
- {
- Free();
- fi.ReSignal();
- }
-
- fSelAnchor = (*fHTE)->selStart;
- TESetClickLoop(fgClickLoopProc, fHTE);
- fText = (*fHTE)->hText;
-
- TEFeatureFlag(teFOutlineHilite, teBitSet, fHTE);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::WriteFields:
- //----------------------------------------------------------------------------------------
- #pragma segment TEWriteResource
-
- void TTEView::WriteFields(TStream* aStream) // override
- {
- Inherited::WriteFields(aStream);
-
- aStream->WriteBoolean(fStyleType);
- aStream->WriteBoolean(fAutoWrap);
- aStream->WriteBoolean(fAcceptsChanges);
- aStream->WriteBoolean(fFreeText);
- aStream->WriteLong(fKeyCommandNumber);
- aStream->WriteInteger(fMaxChars);
- aStream->WriteVRect(fInset);
- aStream->WriteInteger(fJustification);
- aStream->WriteInteger(fTextStyleRsrcID);
- aStream->WriteBoolean(fPreferOutline);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::BecameWindowTarget:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::BecameWindowTarget() // Override
- {
- DoHighlightSelection(fActiveHL, hlOn);
- fActiveHL = hlOn;
-
- Inherited::BecameWindowTarget();
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::BecameTarget:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::BecameTarget() // Override
- {
- SetActive(TRUE);
-
- Inherited::BecameTarget();
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ClickLoop:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- Boolean TTEView::ClickLoop()
- {
- if (StillDown())
- {
- TScroller * scroller = GetScroller(kAnySuperView);
- if (scroller && scroller->Focus())
- {
- CPoint msePt;
- GetMouse(msePt);
-
- VPoint delta;
- scroller->AutoScroll(scroller->QDToViewPt(msePt), delta);// find how much should scroll
- if (Focus())
- {
- CRect visRect(GetVisibleQDRect());
- for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
- {
- short lead = (short)(-visRect[topLeft][vhs]);//!!! long->short cast
- short trail = (short)(fSize[vhs] - visRect[botRight][vhs]);//!!! long->short cast
-
- if (delta[vhs] < 0)
- delta[vhs] = MinMax(lead, delta[vhs], 0);
- else
- delta[vhs] = MinMax(0, delta[vhs], trail);
- }
- // The intent of the above is not to do autoscrolling that would scroll
- // beyond the subview boundary in any direction
-
- if (delta != gZeroVPt)
- {
- scroller->ScrollBy(delta, kRedraw);
- Update(); // make sure the scrolling was visible
- }
- }
- }
-
- // Focus may have changed, which could change lots of things, thus requiring us,
- // tiresomely, to take some or all of the following restorative precautions b/ c
- // clickloop expects us to be clipped to the destrect.
- if (Focus())
- {
- CTemporaryRegion rRgn;
- RectRgn(rRgn, &CRect((*fHTE)->destRect));
- ClipFurtherTo(rRgn, gZeroPt);
- }
- }
-
- return TRUE; // Still consider the mouse to be down
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::AutoScrolling:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::AutoScrolling(Boolean doScrolling)
- {
- if (fHTE)
- TEAutoView(doScrolling, fHTE);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::BeInPort:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::BeInPort(GrafPtr itsPort) // override
- {
- if (fHTE)
- {
- if (itsPort)
- (*fHTE)->inPort = itsPort;
- else
- {
- (*fHTE)->inPort = (GrafPtr)GetWindowPort(gWorkPort);
- DoneTyping();
- fSpecsChanged = TRUE;
- }
- }
-
- Inherited::BeInPort(itsPort);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::BeInScroller:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::BeInScroller(TScroller* itsScroller)// override
- {
- if (fHTE && itsScroller)
- {
- short vertScrollUnit;
-
- if ((*fHTE)->lineHeight > 0) // This works for both old && new TextEdit
- vertScrollUnit = (*fHTE)->lineHeight;
- else
- vertScrollUnit = GetDefFontSize(); // Ask for system default size
-
- itsScroller->SetScrollParameters(VPoint(kStdScrollUnit, vertScrollUnit), FALSE, TRUE);
- }
-
- Inherited::BeInScroller(itsScroller);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::CalcMinFrame:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- VRect TTEView::CalcMinFrame() // override
- {
- // Note that we omit the margins here, so that if TView::ComputeFrame rounds up to a
- // page multiple, the margins will get tacked on after.
- VPoint minSize = VPoint(fSize.h - fInset.left - fInset.right, CalcRealHeight());
-
- if ((fSizeDeterminer[hSel] == sizeVariable) && (!fStyleType && !fAutoWrap))
- minSize = VPoint(fLastWidth, minSize.v);
-
- VRect minFrame(Inherited::CalcMinFrame());
- minFrame[botRight] = minFrame[topLeft] + minSize;
-
- return minFrame;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::CalcRealHeight:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- long TTEView::CalcRealHeight()
- {
- long theHeight = 0; // default return value.
- Boolean lastIsCR;
- short nLines;
- short lineHeight;
-
- CWhileOutlinePreferred setOP(fPreferOutline);
-
- {
- TERec & theTERec = **fHTE;
-
- lastIsCR = (theTERec.teLength <= 0) || ((*(CharsHandle)(theTERec.hText))[theTERec.teLength - 1] == chReturn);
- nLines = theTERec.nLines;
- lineHeight = theTERec.lineHeight;
- }
-
- if (fStyleType != kWithStyle)
- {
- if (lastIsCR)
- ++nLines;
- theHeight = nLines * lineHeight;
- }
- else
- {
- if (nLines > 0)
- theHeight = TEGetHeight(SHRT_MAX, 0, fHTE);
-
- if (lastIsCR) // if lastIsCR, then add to result of TEGetHeight
- {
- short theMode = doAll;
- TextStyle theStyle;
- ContinuousStyle(SHRT_MAX, SHRT_MAX, theMode, theStyle);
-
- FontInfo theFontInfo;
- short theFontHeight;
- GetTextStyleFontInfo(theStyle, theFontInfo, theFontHeight);
-
- theHeight += theFontHeight;
- }
- }
-
- #if qDebugMsg
- if (gIntenseDebugging)
- fprintf(stderr, "CalcRealHeight=%1d\n", theHeight);
- #endif
-
- return theHeight;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::CalcRealWidth:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- long TTEView::CalcRealWidth()
- {
- short fromChar;
- short toChar;
- short aWidth;
- TextStyle theStyle;
- SignedByte savedState;
-
-
- aWidth = 0;
-
- if (!fStyleType)
- {
- if (Focus())
- {
- theStyle = fTextStyle;
- SetPortTextStyle(theStyle);
-
- aWidth = 0;
- fromChar = (*fHTE)->lineStarts[0];
-
- savedState = LockHandle((Handle)(*fHTE)->hText);
-
- for (short index = 1; index <= (*fHTE)->nLines; ++index)
- {
- toChar = (*fHTE)->lineStarts[index] - 1;
- aWidth = (short)Max(aWidth, TextWidth(*((*fHTE)->hText), fromChar, (toChar - fromChar) + 1));//!!! long->short cast
- fromChar = toChar + 1;
- }
-
- HSetState((Handle)(*fHTE)->hText, savedState);
-
- return aWidth;
- }
- }
- else
- {
- #if qDebug
- ProgramBreak("IN TTEView.CalcRealWidth: called for a styled TE Record");
- #endif
-
- }
- return aWidth;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ChangeWrap:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::ChangeWrap(Boolean newAutoWrap,
- Boolean redraw)
- {
- fAutoWrap = newAutoWrap;
- if (newAutoWrap)
- (*fHTE)->crOnly = 0;
- else
- (*fHTE)->crOnly = -1;
- if (redraw)
- {
- RecalcText();
- SynchView(kRedraw);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ComputeFrame:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::ComputeFrame(VRect& newFrame) // override
- {
- Inherited::ComputeFrame(newFrame);
-
- for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
- {
- switch (fSizeDeterminer[vhs])
- {
- case sizeFixed:
- case sizeRelSuperView:
- break;
-
- case sizeSuperView:
- if (fSuperView != NULL)
- break;
- // else drop through to default
- default:
- newFrame[botRight][vhs] += fInset[topLeft][vhs] + fInset[botRight][vhs];
- break;
- }
- }
- // we must limit the frame to kMaxCoord, since Text Edit doesn't allow height to exceed 32K
- newFrame.bottom = newFrame.top + Min(kMaxCoord, newFrame.bottom - newFrame.top);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::CalcSelLoc:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::CalcSelLoc(VRect& selectionRect)
- {
- CharsHandle handleToText;
- short startOfSelection;
- short endOfSelection;
- short charCount;
- short lineHeight;
- short fontAscent;
- TextStyle theStyle;
- Boolean selectionIsTheLastReturn;
- short theMode;
- FontInfo theFontInfo;
- short theFontHeight;
-
- CWhileOutlinePreferred setOP(fPreferOutline);
-
- {
- TERec & theTERec = **fHTE;
-
- charCount = theTERec.teLength;
- startOfSelection = theTERec.selStart;
- endOfSelection = theTERec.selEnd;
- handleToText = (CharsHandle)(theTERec.hText);
-
- if ((theTERec.selEnd - theTERec.selStart == 0) && (fIdleFreq == kMaxIdleTime))
- SetIdleFreq(0); // Idle ASAP
- }
-
- selectionIsTheLastReturn = (startOfSelection == charCount) && (charCount > 0) && ((*handleToText)[charCount - 1] == chReturn);
-
- TEGetStyle(startOfSelection, &theStyle, &lineHeight, &fontAscent, fHTE);
-
- CPoint thePoint(TEGetPoint(startOfSelection, fHTE));
- thePoint.v -= lineHeight;
- selectionRect[topLeft] = QDToViewPt(thePoint);
- selectionRect[botRight] = QDToViewPt(TEGetPoint(endOfSelection, fHTE));
-
- // Through System 6.0.4 TE TEGetPoint returns
- // bogus numbers when the character count is 0.
- if (charCount <= 0)
- {
- selectionRect.top = 0;
- selectionRect.bottom = lineHeight;
- }
-
- if (selectionIsTheLastReturn)
- {
- theMode = doAll;
-
- // Get the style so we know how tall to make the selection when its just the last return
- ContinuousStyle(SHRT_MAX, SHRT_MAX, theMode, theStyle);
-
- GetTextStyleFontInfo(theStyle, theFontInfo, theFontHeight);
-
- selectionRect.top = selectionRect.bottom;
- selectionRect.bottom += theFontHeight;
- }
- else
- {
- // Correct errors by CalcSelLoc. If there is no selection then the "selection"
- // consists of the bits enclosed by the insertion bar.
- if (((*fHTE)->selEnd - (*fHTE)->selStart) == 0)
- selectionRect.left = selectionRect.right - 1;
-
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ContinuousStyle:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- Boolean TTEView::ContinuousStyle(short firstChar,
- short lastChar,
- short& mode,
- TextStyle& aStyle)
- {
- short oldSelStart;
- short oldSelEnd;
- Boolean result;
-
-
- oldSelStart = (*fHTE)->selStart;
- oldSelEnd = (*fHTE)->selEnd;
-
- SetSelect(firstChar, lastChar, fHTE); // Use SetSelect so this is invisible
- result = TEContinuousStyle(&mode, &aStyle, fHTE);
- SetSelect(oldSelStart, oldSelEnd, fHTE); // & so fSpecsChanged isn't touched
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ContainsClipType:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- Boolean TTEView::ContainsClipType(ResType aType)// override
- {
- return (aType == 'TEXT');
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoBreakFollowing: Put in resident segment since this gets called while typing
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- VCoordinate TTEView::DoBreakFollowing(VHSelect vhs,
- VCoordinate previousBreak,
- Boolean& automatic)// override
- {
- VHSelect orthoVhs;
- short possibleLoc;
- TEStyleHandle theStyles;
- LHHandle lhTab;
- short height;
- short lineHeight;
- short lineNo;
- TPrintHandler * itsPrintHandler;
- short countLines;
-
- orthoVhs = gOrthogonal[vhs];
- automatic = TRUE;
-
- if ((itsPrintHandler = GetPrintHandler()) != NULL)
- possibleLoc = (short)Min(kMaxCoord, previousBreak + itsPrintHandler->GetViewPerPage()[orthoVhs]);//!!! long->short
-
- // We want to get rid of the on-screen margin represented by fMargin when printing, so
- // adjust things so that the portion of the view occupied by the screen margin doesn't
- // get printed.
- if (previousBreak == 0)
- possibleLoc += (short)fInset[topLeft][orthoVhs];//!!! long->short
-
- if ((fStyleType == kWithStyle) && (vhs == hSel))
- {
- if (fLastPageBreak == previousBreak)
- {
- height = fLastPageBreak;
- lineNo = fLastLine;
- }
- else
- {
- height = (short)fInset[topLeft][orthoVhs];//!!! VCoordinate->short
- lineNo = 0;
- }
-
- theStyles = TEGetStyleHandle(fHTE);
- lhTab = (*theStyles)->lhTab;
-
- countLines = (*fHTE)->nLines;
- if ((*fHTE)->teLength > 0)
- {
- // we want to see if the last line is empty (only a return)
- // Can't just look at the last byte in hText, because it could be
- // a multi-byte character.
- short offsetOfLastLine = (*fHTE)->lineStarts[countLines - 1];
- char firstByteInLastLine = (*(*fHTE)->hText)[offsetOfLastLine];
- if (firstByteInLastLine == chReturn)
- countLines++;
- }
-
- while (lineNo < countLines)
- {
- lineHeight = (*lhTab)[lineNo].lhHeight;
- if (height + lineHeight <= possibleLoc)
- height += lineHeight;
- else
- break;
- ++lineNo;
- }
- if (lineNo >= countLines)
- possibleLoc = (short)Max(possibleLoc, height);//!!! long->short
- else if (height > previousBreak)
- {
- possibleLoc = height;
- fLastPageBreak = possibleLoc;
- fLastLine = lineNo;
- }
- // else the current line height was bigger than this page--print what we can.
- // next break we'll recalculate from the start again.
- fLastPageBreak = possibleLoc;
- fLastLine = lineNo;
- }
-
- if ((possibleLoc + fInset[topLeft][orthoVhs]) >= fSize[orthoVhs])
- return fSize[orthoVhs];
- else
- return possibleLoc;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoCalcViewPerPage:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- VPoint TTEView::DoCalcViewPerPage()// override
- {
- VPoint returnVal(Inherited::DoCalcViewPerPage()); // Get max amount allowed given margins
-
- #if qDebugMsg
- if (gDebugPrinting)
- fprintf(stderr, "TTEView: incoming generic viewPerPage: %s\n", (const char*)returnVal);
- #endif
-
- // Adjust for integral # of lines per page (unless the view is too small)
- if ((fStyleType != kWithStyle) && fHTE && (*fHTE)->lineHeight > returnVal.v)
- returnVal.v = (*fHTE)->lineHeight * (returnVal.v / (*fHTE)->lineHeight);
-
- #if qDebug
- if (gDebugPrinting)
- fprintf(stderr, "TTEView: computed viewPerPage: %s\n", (const char*)returnVal);
- #endif
-
- return returnVal;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoIdle:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- Boolean TTEView::DoIdle(IdlePhase phase) // override
- {
- if (phase == idleContinue)
- if (fHTE && ((*fHTE)->selEnd - (*fHTE)->selStart == 0) && IsEnabled())
- {
- if (fAcceptsChanges && IsDrawable())
- {
- CWhileOutlinePreferred setOP(fPreferOutline);
- TEIdle(fHTE);
- }
- SetIdleFreq(Max(GetCaretTime() / 2, 1));// Reset idle frequency in case
- // user changed it
- }
- else
- SetIdleFreq(kMaxIdleTime); // No need to bother anyone.
- return FALSE; // Didn't free myself
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoKeyEvent: The Tab character has no width in some fonts, and so can cause
- // confusing screen feedback. It is filtered out by default. If you want to include it in
- // your text union it into fControlChars.
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::DoKeyEvent(TToolboxEvent* event) // override
- {
- TTETypingCommand * aTypingCommand = NULL;
- Boolean needNewCommand = FALSE;
- Boolean handledCharacter = FALSE;
- Boolean selecting = FALSE;
- short goToPos = (*fHTE)->selStart;
- short i = 0;
- short nonAnchor;
- short newStart;
- short newEnd;
- Boolean towardStart;
-
- CStr2 text(event->fText);
-
- if (IsEnabled()) // if view is not enabled then we don't take ANY keystrokes
- {
- if (text >= chSpace || (text < chSpace && macroIn(fControlChars, macroAsSetElem(text[1]))))// Check that the character should be accepted
- {
- if (((text == chLeft) || (text == chRight) || (text == chUp) || (text == chDown)) && Focus())// check for pure movement keys
- {
- DoneTyping(); // Like mousedown, further typing == new command
- fSpecsChanged = TRUE;
- //################
- if ((text != chUp) && (text != chDown))
- fUpDown = FALSE;
-
- if ((text == chLeft) || (text == chRight))
- {
- towardStart = (text == chLeft);
- if (GetScriptVariable((short)GetScriptManagerVariable(smKeyScript), smScriptRight) != 0)//!!! long->short
- towardStart = !towardStart;
- }
- else
- towardStart = FALSE;
-
- {
- TERec & theTERec = **fHTE;
-
- if (theTERec.selStart == theTERec.selEnd)
- fSelAnchor = theTERec.selStart;
- if (fSelAnchor < 0)
- if ((text == chUp) || (towardStart))
- fSelAnchor = theTERec.selEnd;
- else
- fSelAnchor = theTERec.selStart;
- if (fSelAnchor == theTERec.selEnd)
- nonAnchor = theTERec.selStart;
- else
- nonAnchor = theTERec.selEnd;
- }
-
- if ((text == chUp) && (OffsetToLine(nonAnchor) == 0))
- {
- //same as command-Up
- selecting = event->IsShiftKeyPressed();
- handledCharacter = TRUE;
- goToPos = 0;
- fUpDown = FALSE;
- }
- else if ((text == chDown) && (OffsetToLine(nonAnchor) == (*fHTE)->nLines))
- {
- //same as command-Down
- selecting = event->IsShiftKeyPressed();
- handledCharacter = TRUE;
- goToPos = (*fHTE)->teLength;
- fUpDown = FALSE;
- }
-
- if ((!handledCharacter) && (text >= chLeft) && (text <= chDown))
- {
- SignedByte wasState = LockHandle((Handle)fHTE);//??? SRF - why is this getting locked?
-
- selecting = event->IsShiftKeyPressed();
- handledCharacter = TRUE;
-
- if (event->IsCommandKeyPressed())
- {
- if (text == chUp)
- goToPos = 0;
- else if (text == chDown)
- goToPos = (*fHTE)->teLength;
- else if (towardStart)
- goToPos = OffsetToLineStart(nonAnchor);
- else
- goToPos = OffsetToLineEnd(nonAnchor);
- fUpDown = FALSE;
- }
- else if ((text == chUp) || (text == chDown))
- {
- VPoint aVPoint;
-
- if (!fUpDown)
- {
- fUpDown = TRUE;
- aVPoint = OffsetToPt(nonAnchor);
- fUpDownH = (short)aVPoint.h;//!!! VCoordinate->short
- }
- if ((*fHTE)->lineHeight < 0)//styled text
- {
- i = OffsetToLine(nonAnchor);
- i = (short)TEGetHeight(((long)i), ((long)i), fHTE);//!!! long->short
- }
- else
- i = (*fHTE)->lineHeight;
- if (text == chUp)
- i = -i;
- aVPoint = OffsetToPt(nonAnchor);
- aVPoint.v += i;
- aVPoint.h = fUpDownH;
- goToPos = PtToOffset(aVPoint);
- i = OffsetToLine(nonAnchor);
- if ((text == chUp) && (goToPos == (*fHTE)->lineStarts[i]))
- --goToPos;
- else if ((text == chDown) && (OffsetToLine(goToPos) == i + 2))
- ++goToPos;
- }
- else if (event->IsOptionKeyPressed())
- {
- Boolean expanding;
-
- if (selecting)
- if (towardStart)
- expanding = (nonAnchor <= fSelAnchor);
- else
- expanding = (nonAnchor >= fSelAnchor);
- else
- expanding = TRUE;
- goToPos = nonAnchor;
- if (!expanding)
- if (towardStart)
- {
- newEnd = goToPos;
- while ((goToPos > fSelAnchor) && ((!WordBounds(goToPos, newStart, newEnd)) || (newEnd + 1 >= nonAnchor)))
- goToPos = newStart - 1;
- if (goToPos <= fSelAnchor)
- {
- expanding = TRUE;
- goToPos = fSelAnchor;
- }
- else
- goToPos = newEnd + 1;
- }
- else //!towardStart
- {
- newStart = goToPos;
- while ((goToPos < fSelAnchor) && ((!WordBounds(goToPos, newStart, newEnd)) || (newStart <= nonAnchor)))
- goToPos = newEnd + 1;
- if (goToPos >= fSelAnchor)
- {
- expanding = TRUE;
- goToPos = fSelAnchor;
- }
- }
- if (expanding)
- if (towardStart)
- {
- i = goToPos;
- newStart = goToPos;
- while ((goToPos > 0) && ((!WordBounds(goToPos, newStart, newEnd)) || (newStart >= i)))
- --goToPos;
- goToPos = (short)Min(goToPos, newStart);//!!! long->short
- }
- else //!towardStart
- {
- newEnd = goToPos;
- i = (*fHTE)->teLength - 1;
- while ((goToPos < i) && !WordBounds(goToPos + 1, newStart, newEnd))
- ++goToPos;
- goToPos = (short)Max(goToPos, newEnd) + 1;//!!! long->short
- }
- }
- else if (event->IsShiftKeyPressed() || ((*fHTE)->selStart == (*fHTE)->selEnd))
- {
- if (towardStart)
- goToPos = GetPrevCharacterOffset(nonAnchor);
- else
- goToPos = GetNextCharacterOffset(nonAnchor);
- }
- else // selStart != selEnd && ((text == chLeft)
- // || (text == chRight))
- {
- if (towardStart)
- goToPos = (*fHTE)->selStart;
- else
- goToPos = (*fHTE)->selEnd;
- }
-
- HSetState((Handle)fHTE, wasState);
-
- }
-
- handledCharacter = TRUE;
- if (selecting)
- SetSelection((short)Min(goToPos, fSelAnchor), (short)Max(goToPos, fSelAnchor), kRedraw);//!!! long->short
- else
- SetSelection(goToPos, goToPos, kRedraw);
- ScrollSelectionIntoView(kRedraw);
- }
- else if (fAcceptsChanges && Focus())
- {
- // Check max size for text, and that we're not running out of memory
- if ((text != chBackspace) && (text != chFwdDelete) && ((*fHTE)->selStart == (*fHTE)->selEnd))
- if (((fMaxChars - GetHandleSize(fText)) < 1) || MemSpaceIsLow())
- {
- StdAlert(phTooManyChars);
- // exit(DoKeyEvent); // Flush further keystrokes
- return;
- }
-
- // This key will change the horizontal position of the insertion point
- fUpDown = FALSE;
-
- // Pass the character to the typing command, creating a new one if necessary
- needNewCommand = (fTypingCommand == NULL);
- if (!needNewCommand)
- needNewCommand = fTypingCommand->fCompleted;
-
- if (needNewCommand)
- {
- aTypingCommand = DoMakeTypingCommand(text);
- fTypingCommand = aTypingCommand;
- PostCommand(aTypingCommand);
- }
- else
- {
- short oldLength = (*fHTE)->teLength;
- fTypingCommand->AddCharacter(text);
- // Once you're typing (first character already processed) collecting
- // subsequent characters really shouldn't affect the menus unless
- // you're keeping a character count in them or something that depends
- // on the aggregate of the characters you've typed. The TextStyle for
- // the first character would certainly apply to subsequent characters.
- // So, this is one ideal place to say that the event _DOES NOT_ affect
- // the menus. If the menus are already invalid they will stay so, but
- // if they are valid then there is no need to invalidate them in the
- // character collection process. When the user terminates the addition
- // of characters with another event, the menus will be setup from that
- // event. If you really feel you must, then you can always override,
- // call Inherited and then set event->fAffectMenus back to true.
- // (Rhymes with rue)
- event->fAffectsMenus = ((*fHTE)->teLength == 0 || oldLength == 0);
- }
- handledCharacter = TRUE;
- }
- }
- if (fIdleFreq == kMaxIdleTime)
- SetIdleFreq(0); // Idle ASAP, since someone may somehow set an
- // insertion point and want it to flash. (the
- // idle time will be reset to match the caret
- // time in doidle.)
- }
-
- if (!handledCharacter)
- Inherited::DoKeyEvent(event);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SetSelection:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::SetSelection(short newSelStart,
- short newSelEnd,
- Boolean redraw)
- {
- if (redraw && Focus())
- {
- short oldSelStart = (*fHTE)->selStart;
- short oldSelEnd = (*fHTE)->selEnd;
-
- CWhileOutlinePreferred setOP(fPreferOutline);
- TESetSelect(Max(newSelStart, 0), Min(newSelEnd, (*fHTE)->teLength), fHTE);
-
- #if qDrag
- if (GetDraggable())
- gDispatcher->InvalidateMouseRegions();
- #endif
-
- }
- else
- SetSelect((short)Max(newSelStart, 0), (short)Min(newSelEnd, (*fHTE)->teLength), fHTE);//!!! long->short
-
- if (newSelStart == newSelEnd)
- fSelAnchor = newSelStart;
-
- fSpecsChanged = TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoMakeEditCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment TESelCommand
-
- TTECommand* TTEView::DoMakeEditCommand(CommandNumber aCommandNumber)
- {
- TTECommand * aTECommand = NULL;
-
- switch (aCommandNumber)
- {
- case cCut:
- case cCopy:
- aTECommand = new TTECutCopyCommand;
- ((TTECutCopyCommand *)aTECommand)->ITECutCopyCommand(this, aCommandNumber);
- break;
-
- case cPaste:
- aTECommand = new TTEPasteCommand;
- ((TTEPasteCommand *)aTECommand)->ITEPasteCommand(this);
- break;
-
- case cClear:
- aTECommand = new TTECommand;
- aTECommand->ITECommand(this, aCommandNumber, TRUE);
- break;
-
- }
-
- return aTECommand;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoMakeStyleCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- TTEStyleCommand* TTEView::DoMakeStyleCommand(const TextStyle& aStyle,
- CommandNumber itsCommandNumber,
- short itsMode)
- {
- TTEStyleCommand * aTEStyleCommand = new TTEStyleCommand;
-
- aTEStyleCommand->ITEStyleCommand(this, aStyle, itsCommandNumber, itsMode);
- return aTEStyleCommand;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoMakeTypingCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- TTETypingCommand* TTEView::DoMakeTypingCommand(const CStr2& ch)
- {
- TTETypingCommand * aTypingCommand = new TTETypingCommand;
-
- aTypingCommand->ITETypingCommand(this, ch);
- return aTypingCommand;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoMenuCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment TESelCommand
-
- void TTEView::DoMenuCommand(CommandNumber aCommandNumber)// override
- {
- switch (aCommandNumber)
- {
- case cCut:
- case cCopy:
- case cClear:
- {
- fUpDown = FALSE;
- PostCommand(DoMakeEditCommand(aCommandNumber));
- break;
- }
-
- case cPaste:
- {
- long nChars;
- ResType dataType = '%%%%';
-
- nChars = gClipboardMgr->GetDataToPaste(NULL, dataType);
- if (dataType == 'TEXT')
- {
- if (nChars < 0)
- {
- #if qDebug
- ProgramBreak("Couldn't get data to paste");
- #endif
-
- }
- else
- {
- if (nChars - ((*fHTE)->selEnd - (*fHTE)->selStart) > fMaxChars - GetHandleSize(fText))
- {
- OSErr anErr;
- anErr = MAInteractWithUser();
- if (anErr == noErr)
- StdAlert(phTooManyChars);
- else if (anErr == errAENoUserInteraction)
- FailOSErr(errTooManyChars);
- else
- FailOSErr(anErr);
- }
- else
- {
- fUpDown = FALSE;
- PostCommand(DoMakeEditCommand(aCommandNumber));
- }
- }
- }
- else
- {
- Inherited::DoMenuCommand(aCommandNumber);
- }
- break;
- }
-
- case cSelectAll:
- {
- if (Focus())
- {
- fUpDown = FALSE;
- SetSelection(0, (*fHTE)->teLength, kRedraw);
- DoneTyping();
- fSpecsChanged = TRUE;
- ScrollSelectionIntoView(kRedraw);
- }
- break;
- }
-
- default:
- {
- Inherited::DoMenuCommand(aCommandNumber);
- break;
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoMouseCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::DoMouseCommand(VPoint& theMouse,
- TToolboxEvent* event,
- CPoint) // override
- {
- if (Focus() && IsVisible())
- {
- CWhileOutlinePreferred setOP(fPreferOutline);
-
- fgCurrTEView = this; // So the global clickLoop routine can forward
- DoneTyping(); // Mousedown terminates the Typing command
- fUpDown = FALSE; // and resets the horizontal location for up
- // and down arrow cursor movement
- fSpecsChanged = TRUE;
- TEClick(ViewToQDPt(theMouse), event->IsShiftKeyPressed(), fHTE);
-
- // …force a re-focus b/c the focusing in ClickLoop clips down to the destrect.
- if (IsFocused())
- InvalidateFocus();
-
- #if qDrag
- // force an update of the current sleep region and cursor
- gDispatcher->InvalidateCursorRgn();
- #endif // qDrag
-
- fSelAnchor = (*fHTE)->selStart; // Remember the new selection anchor
-
- if (fIdleFreq == kMaxIdleTime)
- SetIdleFreq(0); // Idle ASAP
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoneTyping:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::DoneTyping()
- {
- if (fTypingCommand)
- {
- fTypingCommand->CompleteTyping();
- fTypingCommand = NULL;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoSetPageOffset:
- //----------------------------------------------------------------------------------------
- #pragma segment TEPrint
-
- void TTEView::DoSetPageOffset(const VPoint& coord)// override
-
- {
- Inherited::DoSetPageOffset(coord);
- for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
- if (coord[vhs] == 0)
- TPrintHandler::gPageOffset[vhs] += fInset[topLeft][vhs];
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoSetupMenus:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::DoSetupMenus() // override
- {
- Boolean manyChars;
-
- Inherited::DoSetupMenus();
-
- manyChars = (*fHTE)->selStart < (*fHTE)->selEnd;
- if (!MemSpaceIsLow())
- {
- if (fAcceptsChanges) // One way or another, we can paste text
- gClipboardMgr->CanPaste('TEXT'); // If styles exist, all the better
-
- Enable(cCopy, manyChars);
- }
- Enable(cSelectAll, (*fHTE)->teLength > 0);
-
- // We enable Cut even if space is low, since it's nice to be able to rescue some of
- // the stuff you have to delete even if space is low. Note that it is possible to get
- // into the "can't do any commands" situation as a result. You should be able to close
- // and save the big document, then save the rescued text elsewhere, however.
- Enable(cCut, manyChars && fAcceptsChanges);
- Enable(cClear, manyChars && fAcceptsChanges);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::Draw:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::Draw(const VRect& area) // override
- {
- short oldHilite;
- short oldCaretState;
-
- CWhileOutlinePreferred setOP(fPreferOutline);
-
- Boolean wasActive = ((Boolean)((*fHTE)->active != 0));
- Boolean hideSelection = (gPrinting || gDrawingPictScrap);
-
- if (hideSelection)
- {
- // we don't TEDeactivate to draw the selection as an outline
- oldHilite = TEFeatureFlag(teFOutlineHilite, teBitClear, fHTE);
-
- // we have to remember what the caret state was, because
- // TEDeactivate is going to blast it
- oldCaretState = (*fHTE)->caretState;
-
- // …prevent selection from being drawn.
- CTemporaryRegion tempRgn;
-
- GetClip(tempRgn);
- ClipRect(gZeroRect);
- TEDeactivate(fHTE);
- SetClip(tempRgn);
- }
-
- TEUpdate(&ViewToQDRect(area), fHTE); // normal screen update handled by
- // TextEdit directly
-
- if (hideSelection && wasActive)
- {
- // Reactivate the TE record if we deactivated it
- if (wasActive)
- {
- CTemporaryRegion tempRgn;
-
- GetClip(tempRgn);
- ClipRect(gZeroRect);
- TEActivate(fHTE);
- SetClip(tempRgn);
- }
-
- TEFeatureFlag(teFOutlineHilite, oldHilite, fHTE);
-
- // Put the caret state back to what it was
- (*fHTE)->caretState = oldCaretState;
- }
-
- Inherited::Draw(area);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ExtractStyles:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::ExtractStyles(TEStyleHandle& theStyles,
- STHandle& theElements)
- {
- theStyles = TEGetStyleHandle(fHTE);
- theElements = (*theStyles)->styleTab;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ExtractText:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- Handle TTEView::ExtractText() const
- {
- return fText;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetPrintExtent:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- VRect TTEView::GetPrintExtent()// override
- {
- VRect printExtent(Inherited::GetPrintExtent());
-
- printExtent[topLeft] += fInset[topLeft];
- printExtent[botRight] -= fInset[botRight];
-
- return printExtent;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetSelectionString:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::GetSelectionString(CStr255& selection)
- {
- selection.Empty();
- Handle theChars = fText;
- if (theChars && fHTE)
- {
- short start = (*fHTE)->selStart;
- short length = (short)Min(kStr255Len, (*fHTE)->selEnd - start);//!!! long->short
- if (length > 0)
- selection.CopyFrom((void*)((*theChars) + start), length);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetNumberOfChars:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::GetNumberOfChars()
- {
- short numOfChars = 0;
-
- if (fText)
- {
- Handle theText = fText;
- short textSize = (short)GetHandleSize(theText);
-
- for (short numOfBytes = 0; numOfBytes < textSize; ++numOfBytes)
- if (MACharacterByteType(*theText, numOfBytes, smCurrentScript) != smFirstByte)
- ++numOfChars;
- }
-
- return numOfChars;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetCharacterOffset:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::GetCharacterOffset(short fromOffset)
- {
- return (MACharacterByteType(*fText, fromOffset, smCurrentScript) != smLastByte) ? fromOffset : --fromOffset;
- }
-
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetBytesInCharacter:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::GetBytesInCharacter(short inOffset)
- {
- // return the correct number of bytes even if the offset points into the middle
- // of a two byte character
- return (MACharacterByteType(*fText, inOffset, smCurrentScript) == smSingleByte) ? 1 : 2;
- }
-
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetNextCharacterOffset:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::GetNextCharacterOffset(short fromOffset)
- {
- short correctedCharacterOffset = GetCharacterOffset(fromOffset);
-
- return Min (correctedCharacterOffset + GetBytesInCharacter(correctedCharacterOffset), (*fHTE)->teLength);
- }
-
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetPrevCharacterOffset:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::GetPrevCharacterOffset(short fromOffset)
- {
- short correctedCharacterOffset = GetCharacterOffset(fromOffset);
-
- return Max (correctedCharacterOffset - GetBytesInCharacter(correctedCharacterOffset), 0);
- }
-
-
- //----------------------------------------------------------------------------------------
- // TTEView::GivePasteData:
- //----------------------------------------------------------------------------------------
- #pragma segment TEClipboard
-
- long TTEView::GivePasteData(Handle aDataHandle,
- ResType dataType) // override
- {
- short oldStart;
- short oldEnd;
- long aSize = 0;
- MAVolatileInit(Handle, aHandle, NULL);
- OSErr err;
- Boolean savedPerm = FALSE;
- SignedByte savedState;
-
- FailInfo fi;
- Try(fi)
- {
- if (dataType == 'TEXT')
- {
- aSize = GetHandleSize(fText);
- if (aDataHandle)
- {
- SetPermHandleSize(aDataHandle, aSize);// Don't forget. This can fail
- MABlockMove((*fText), (*aDataHandle), aSize);
- }
- }
- else if (dataType == 'styl')
- {
- if (fStyleType == kWithStyle)
- if (!SpaceForStyles(0, SHRT_MAX))
- Failure(noErr, 0); // We'll accept this error in worst case
- else
- {
- oldStart = (*fHTE)->selStart;
- oldEnd = (*fHTE)->selEnd;
- SetSelect(0, SHRT_MAX, fHTE);
- aHandle = (Handle)TEGetStyleScrapHandle(fHTE);
- SetSelect(oldStart, oldEnd, fHTE);
-
- if (aHandle)
- {
- aSize = GetHandleSize(aHandle);
- if (aDataHandle)
- {
- savedPerm = PermAllocation(TRUE);
- // Try to prevent fragmentation, in case can't move while we're copying it!
- savedState = LockHandleHigh(aHandle);
- // Copy styles into user-supplied handle
- err = PtrToXHand((*aHandle), aDataHandle, aSize);
- HSetState(aHandle, savedState);// Okay for it to move again
- savedPerm = PermAllocation(savedPerm);
- if (err != noErr) // Maybe enough for one copy, but not two!
- Failure(phStylesTooBig, phStylesTooBig + messageAlert);
- }
- aHandle = DisposeIfHandle(aHandle);
- }
- else if (aDataHandle) // Hmm. There _was_ enough memory, but the
- Failure(phStylesTooBig, phStylesTooBig + messageAlert);// heap is
- // probably
- // pretty
- // fragmented
-
- }
- }
- else
- Failure(noTypeErr, 0);
-
- FailSpaceIsLow();
- fi.Success();
- }
- else // Recover
- {
- aHandle = DisposeIfHandle(aHandle);
- fi.ReSignal();
- }
-
- return aSize;
- }
-
-
- //----------------------------------------------------------------------------------------
- // TTEView::MakeTERecord:
- //----------------------------------------------------------------------------------------
- #pragma segment TEOpen
-
- void TTEView::MakeTERecord()
- {
- TEHandle anHTE;
- GrafPtr oldPort;
- TextStyle aTextStyle;
-
- GetPort(&oldPort);
- SetPortWindowPort(gWorkPort);
- aTextStyle = fTextStyle;
- SetPortTextStyle(aTextStyle);
-
- VRect itsExtent(GetExtent());
- itsExtent[topLeft] += fInset[topLeft];
- itsExtent[botRight] -= fInset[botRight];
-
- CRect dest(ViewToQDRect(itsExtent));
-
- if (fStyleType == kWithStyle)
- anHTE = TEStyleNew(dest, &dest); // Open a styled record if requested
- else
- anHTE = TENew(&dest, &dest); // …otherwise, do it the old way
-
- SetPort(oldPort);
-
- FailNIL(anHTE); // Make sure we actually created one
- fHTE = anHTE; // We did, so save off TE handle
-
- if (fStyleType == kWithStyle)
- {
- TEStyleHandle styleHandle = TEGetStyleHandle(fHTE);
- (*styleHandle)->teRefCon = (long)this; // set the refcon to this view
- }
-
- fgDefClickLoopProc = (*anHTE)->clickLoop; // Just in case we want to restore it…
- // Note that the system call SetClickLoop
- // _cannot_ be used to set the clickProc of
- // the TERecord. This is because the default
- // clickproc does not follow the Pascal Parameter
- // passing conventions that this procedure expects.
-
- SetJustification(fJustification, kDontRedraw);// Set justification to requested value
- ChangeWrap(fAutoWrap, FALSE); // Install auto wrap (or CR only)
- FailNoReserve(); // Got to have some reserve tank
-
- BeInPort(GetGrafPort()); // Associate with real port
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::OffsetToLine:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::OffsetToLine(short offset)
- {
- TERec & theTERec = **fHTE;
-
- if (theTERec.nLines <= 1)
- return 0;
- else
- {
- short i;
- for (i = 0; i < theTERec.nLines; i++)
- if (theTERec.lineStarts[i] >= offset)
- break;
-
- return i;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::OffsetToLineStart:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::OffsetToLineStart(short offset)
- {
- short returnVal = 0;
-
- if ((*fHTE)->nLines > 1)
- {
- short i = OffsetToLine(offset);
- returnVal = (*fHTE)->lineStarts[i];
- }
-
- return returnVal;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::OffsetToLineEnd:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::OffsetToLineEnd(short offset)
- {
- TERec & theTERec = **fHTE;
- short returnVal = theTERec.teLength;
-
- if (theTERec.nLines > 1)
- {
- short i;
- for (i = 0; i < theTERec.nLines; i++)
- {
- if (theTERec.lineStarts[i] >= offset)
- break;
- }
-
- if (i < theTERec.nLines - 1)
- returnVal = (theTERec.lineStarts[i + 1] - 1);
- }
-
- return returnVal;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::OffsetToPt:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- VPoint TTEView::OffsetToPt(short offset)
- {
- short height;
- TextStyle theStyle;
- short ascent;
-
- TEGetStyle(offset, &theStyle, &height, &ascent, fHTE);
-
- // Through System 6.0.4 TEGetPoint returns bogus numbers when the character count is 0.
- CPoint thePoint;
- if ((*fHTE)->teLength <= 0)
- {
- thePoint = fInset[topLeft].ToPoint();
- thePoint.v += ascent;
- }
- else
- {
- thePoint = TEGetPoint(offset, fHTE);
- if ((!TEIsFrontOfLine(offset, fHTE)) || ((*((*fHTE)->hText))[offset - 1] != chReturn))
- thePoint.v += ascent - height;
- }
-
- return QDToViewPt(thePoint);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::PtToOffset:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- short TTEView::PtToOffset(const VPoint& aPoint)
- {
- CPoint aQDPoint;
- short offset;
-
- aQDPoint = ViewToQDPt(aPoint);
- offset = TEGetOffset(aQDPoint, fHTE);
-
- if ((TEIsFrontOfLine(offset, fHTE)) && (offset) && ((*((*fHTE)->hText))[offset - 1] == chReturn) && (TEGetPoint(offset - 1, fHTE).h < aQDPoint.h))
- {
- offset--;
- }
-
- return offset;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::RecalcText:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::RecalcText()
- {
- fUpDown = FALSE;
- TECalText(fHTE);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ResignedWindowTarget:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::ResignedWindowTarget() // override
- {
- DoHighlightSelection(fActiveHL, hlDim);
- fActiveHL = hlDim;
-
- Inherited::ResignedWindowTarget();
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ResignedTarget:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::ResignedTarget() // override
- {
- SetActive(FALSE);
-
- Inherited::ResignedTarget();
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::InvalidateFrameDifference:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::InvalidateFrameDifference(const VRect& oldFrame,
- const VRect& newFrame)
- {
- // Account for the inset
-
- VRect theOldFrame(oldFrame[topLeft] + fInset[topLeft], oldFrame[botRight] - fInset[botRight]);
- VRect theNewFrame(newFrame[topLeft] + fInset[topLeft], newFrame[botRight] - fInset[botRight]);
-
- Inherited::InvalidateFrameDifference(theOldFrame, theNewFrame);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SetFrame:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::SetFrame(const VRect& newFrame,
- Boolean invalidate) // override
- {
- VPoint oldSize(fSize);
-
- Inherited::SetFrame(newFrame, invalidate);
-
- if (fHTE && fSize != oldSize)
- {
- CRect itsQDExtent(GetQDExtent());
-
- CRect r(itsQDExtent[topLeft] + (fInset[topLeft].ToPoint()), (itsQDExtent[botRight] - (fInset[botRight]).ToPoint()));
-
- Boolean needCalText = (fSize.h != oldSize.h) || (r.right != (*fHTE)->destRect.right);
-
- StuffTERects(r);
- if (needCalText)
- {
- RecalcText();
- SynchView(kDontRedraw);
- short actualJust = GetActualJustification(fJustification);
- if (invalidate && ((fAutoWrap && (fSize != oldSize)) || ((actualJust == teFlushRight) || (actualJust == teCenter))))
- ForceRedraw();
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::UpdateCoordinates:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::UpdateCoordinates() // override
- {
- CPoint oldQDOrigin(fQDOrigin);
- VPoint oldViewToQDOffset(fViewToQDOffset);
-
- Inherited::UpdateCoordinates();
-
- if (fQDOrigin != oldQDOrigin || fViewToQDOffset != oldViewToQDOffset)
- if (fHTE)
- {
- CRect itsQDExtent(GetQDExtent());
-
- StuffTERects(CRect(itsQDExtent[topLeft] + (fInset[topLeft].ToPoint()), (itsQDExtent[botRight] - (fInset[botRight]).ToPoint())));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ScrollSelectionIntoView:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::ScrollSelectionIntoView(Boolean redraw)// Override
- {
- if ((GetScroller(kAnySuperView) != NULL) && Focus())// Can't scroll selection if we don't have
- {
- // … a scroller!
- if (fIdleFreq == kMaxIdleTime)
- SetIdleFreq(0); // Idle ASAP
-
- VRect selectionRect;
- CalcSelLoc(selectionRect);
-
- #if qDebugMsg
- if (gIntenseDebugging)
- {
- fprintf(stderr, "Visible CRect was: %s", (const char*)GetVisibleRect());
- fprintf(stderr, "; Sel CRect was: %s\n", (const char*)selectionRect);
- }
- #endif
-
- if (!GetVisibleRect().Contains(selectionRect))
- {
- // Scroll the selection into view. accounting for the user's preference for
- // how much to jump at a time with fMinAhead. */
- VPoint minToSee(Min(fMinAhead, fSize.h - selectionRect.left), selectionRect.GetLength(vSel));
- #if qDebug
- if (gIntenseDebugging)
- {
- fprintf(stderr, "RevealRect: r = %s", (const char*)selectionRect);
- fprintf(stderr, ", minToSee = %s\n", (const char*)minToSee);
- }
- #endif
-
- RevealRect(selectionRect, minToSee, redraw);
- Focus(); // Refocus in newly-scrolled position.
- }
- }
- else if (!fAutoWrap && fHTE)
- TESelView(fHTE);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SetJustification:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::SetJustification(short newJust,
- Boolean redraw)
- {
- TESetAlignment(newJust, fHTE);
- fJustification = newJust;
-
- CRect itsQDExtent(GetQDExtent());
-
- StuffTERects(CRect(itsQDExtent[topLeft] + (fInset[topLeft].ToPoint()), (itsQDExtent[botRight] - (fInset[botRight]).ToPoint())));
-
- if (redraw)
- ForceRedraw();
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SetOneStyle:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::SetOneStyle(short theStart,
- short theEnd,
- short theMode,
- const TextStyle& theStyle,
- Boolean redraw)
- {
- short saveStart;
- short saveEnd;
- FontInfo fInfo;
- TextStyle newStyle;
- short theFontHeight;
-
- CWhileOutlinePreferred setOP(fPreferOutline);
-
- InvalidateFocus();
- Focus();
- if (fStyleType == kWithStyle)
- {
- saveStart = (*fHTE)->selStart;
- saveEnd = (*fHTE)->selEnd;
- SetSelect(theStart, theEnd, fHTE);
- TESetStyle(theMode, &theStyle, redraw, fHTE);
- SetSelect(saveStart, saveEnd, fHTE);
- }
- else
- {
- if (theMode == doAll)
- newStyle = theStyle;
- else
- {
- newStyle = fTextStyle;
- if (theMode & doFont)
- {
- newStyle.tsFont = theStyle.tsFont;
-
- KeyScript(FontToScript(newStyle.tsFont));// …keybd input system to match new font
- }
- if (theMode & doFace)
- newStyle.tsFace = theStyle.tsFace;
- if (theMode & doColor)
- newStyle.tsColor = theStyle.tsColor;
- if (theMode & addSize)
- newStyle.tsSize += theStyle.tsSize;
- else if (theMode & doSize)
- newStyle.tsSize = theStyle.tsSize;
- }
-
- GetTextStyleFontInfo(newStyle, fInfo, theFontHeight);// Need to get font's
- // height and ascent.
-
- {
- TERec & theTERec = **fHTE;
-
- theTERec.txSize = newStyle.tsSize;
- theTERec.txFont = newStyle.tsFont;
- theTERec.txFace = newStyle.tsFace;
- theTERec.fontAscent = fInfo.ascent;
- theTERec.lineHeight = theFontHeight;
- }
- SetIfColor(newStyle.tsColor);
-
- fTextStyle = newStyle;
- }
-
- RecalcText();
- SynchView(redraw && (fStyleType == kWithStyle));
- if (redraw && (fStyleType == kWithoutStyle))
- ForceRedraw();
-
- fSpecsChanged = TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SetText:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::SetText(const CStr255& theText)
- {
- Handle theTextHandle;
-
- if (fHTE) // If we're replacing text, styles are kaput
- {
- CStr255 localText(theText);
- OSErr err;
-
- Boolean allocationState = PermAllocation(TRUE);
- err = PtrToHand((Ptr) & localText[1], &theTextHandle, theText.Length());
- PermAllocation(allocationState);
-
- FailOSErr(err);
-
- StuffText(theTextHandle);
-
- // Set the insertion point to the end of the text.
- // (This is consistant with the TextEdit SetText routine)
- long textLength = GetHandleSize(theTextHandle);
- SetSelection((short)textLength, (short)textLength, kDontRedraw);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::ShowReverted:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::ShowReverted() // override
- {
- RecalcText();
- fLastHeight = 0;
- fLastWidth = 0;
- Inherited::ShowReverted();
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SpaceForStyles:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- Boolean TTEView::SpaceForStyles(long rangeStart,
- long rangeEnd)
- {
- FailInfo fi;
- Try(fi)
- {
- Handle h = NewPermHandle(TENumStyles(rangeStart, rangeEnd, fHTE) * sizeof(ScrpSTElement) + 2);
-
- h = DisposeIfHandle(h); // Release memory back to the system
- fi.Success();
- return TRUE;
- }
- else // Recover
- {
- StdAlert(phStylesTooBig);
- // Don't resignal in this case
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::StuffStyles:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::StuffStyles(TEStyleHandle theStyles,
- STHandle theElements)
- {
- if ((fStyleType == kWithStyle) && fHTE)
- {
- LHHandle oldLineHeights;
- NullStHandle theNullStyles;
- StScrpHandle theScrpHandle;
- long savedRefCon;
-
- {
- TEStyleHandle oldStyles = TEGetStyleHandle(fHTE);
- TEStyleRec & theTEStyleRec = **oldStyles;
-
- STHandle oldElements = theTEStyleRec.styleTab;
- oldLineHeights = theTEStyleRec.lhTab;
- theNullStyles = theTEStyleRec.nullStyle;
- theScrpHandle = (*theTEStyleRec.nullStyle)->nullScrap;
- savedRefCon = theTEStyleRec.teRefCon;
- oldElements = (STHandle)DisposeIfHandle((Handle)oldElements);
- }
-
- {
- TEStyleRec & theTEStyleRec = **theStyles;
-
- theTEStyleRec.styleTab = theElements;// Replace STElements handle
- theTEStyleRec.lhTab = oldLineHeights;// Replace line heights table handle
- theTEStyleRec.nullStyle = theNullStyles;// Replace NULL style handle
- (*theTEStyleRec.nullStyle)->nullScrap = theScrpHandle;
- theTEStyleRec.teRefCon = savedRefCon;// restore the refcon reference
- }
-
- // NOTE!! TESetStyleHandle will dispose of oldStyles for us!
- TESetStyleHandle(theStyles, fHTE);
-
- RecalcText();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::StuffText:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::StuffText(Handle theText)
- {
- if (fHTE) // If we're replacing text, styles are kaput
- {
- long textLength = GetHandleSize(theText);// Check size of new text
-
- // Remember where the selection range was & try to keep it
- // in the same state when the new text is added. Usually, the
- // selection range should be in one of three states: at the
- // beginning of the text, at the end of the text, or covering
- // all of the text (everything selected). This isn't enforced,
- // though.
-
- short oldStart = (*fHTE)->selStart;
- short oldEnd = (*fHTE)->selEnd;
- if ((oldStart >= textLength) || (oldStart >= (*fHTE)->teLength))
- oldStart = (short)textLength;
- if ((oldEnd >= textLength) || (oldEnd >= (*fHTE)->teLength))
- oldEnd = (short)textLength;
-
- if (textLength > fMaxChars)
- {
- #if qDebug
- ProgramBreak("Text size exceeds maximum for this view");
- #endif
-
- Failure(minErr, 0);
- }
-
- // Set the selection range to cover the entire text,
- // then call TESetStyle to fix up the style table.
- // It is _very important_ to reduce the number of style runs
- // down to one _before_ changing (*fHTE)->hText because
- // TESetStyle is not smart enough to fix up the style
- // run table if its length differs from (*fHTE)->teLength.
-
- if (fStyleType == kWithStyle)
- {
- SetSelect(0, SHRT_MAX, fHTE);
- TextStyle aTextStyle = fTextStyle;
- TESetStyle(doAll, &aTextStyle, FALSE, fHTE);
- }
-
- if ((fSavedTEHandle != theText) && ((*fHTE)->hText != theText))
- {
- fSavedTEHandle = DisposeIfHandle(fSavedTEHandle);// …we have no choice but to dispose it
- fSavedTEHandle = (*fHTE)->hText; // Save existing handle
- (*fHTE)->hText = theText; // Install new handle
- }
-
- fText = theText; // Make a local copy, too
- (*fHTE)->teLength = (short)textLength; // Tell TE how long we are //!!! long->short
-
- if (fStyleType == kWithStyle)
- {
- // At this point we are back to one plain-text
- // style, but TECalText is not smart enough to change
- // the total length of the style run table, so we
- // must do it manually here.
-
- TEStyleHandle styles = TEGetStyleHandle(fHTE);
- TEStyleRec & aTEStyleRec = **styles;
-
- // This next line sets the "dummy record" at the end of the table. See IM V-261.
- aTEStyleRec.runs[1].startChar = (*fHTE)->teLength + 1;
- }
-
- RecalcText(); // Recalculate the line breaks and line-height tables
- SetSelect(oldStart, oldEnd, fHTE); // Restore the old selection
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::StuffTERects:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::StuffTERects(const CRect& newTERect)
- {
- CWhileOutlinePreferred setOP(fPreferOutline);
-
- TextStyle aTextStyle = fTextStyle;
-
- FontInfo aFontInfo;
- short theFontHeight;
- GetTextStyleFontInfo(aTextStyle, aFontInfo, theFontHeight);
-
- CRect localRect(newTERect);
- short actualJust = GetActualJustification(fJustification);
- if ((fSizeDeterminer[hSel] == sizeVariable) && (actualJust != teJustCenter) && !fStyleType && !fAutoWrap)
- {
- if ((actualJust == teJustLeft) || (actualJust == teForceLeft))
- localRect.right = localRect.right + aFontInfo.widMax;
- else
- localRect.left = localRect.left - aFontInfo.widMax;
- }
- else
- localRect.right = (short)Max(localRect.right, localRect.left + aFontInfo.widMax);//!!! long->short
-
- (*fHTE)->destRect = localRect;
- (*fHTE)->viewRect = localRect;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SynchView:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::SynchView(Boolean redraw)
-
- {
- const short kInsertionBarWidth = 1; // insertion bar is one pixel wide
-
- long theHeight = CalcRealHeight();
-
- long theWidth;
- Boolean doRealWidth = (fSizeDeterminer[hSel] == sizeVariable) && !fStyleType && !fAutoWrap;
- if (doRealWidth)
- theWidth = CalcRealWidth() + kInsertionBarWidth * 2;
-
- if ((fLastHeight != theHeight) || (doRealWidth && (fLastWidth != theWidth)))
- {
- if (doRealWidth)
- fLastWidth = theWidth; // Width is expensive to calculate. Cache
- // for CalcMinFrame
-
- AdjustFrame(); // may need to grow view
- fLastHeight = theHeight; // Remember new height value
- }
-
- // First, make sure selection is visible (this conveniently focuses). Then, repair any
- // extra feedback which TextEdit may have mashed.
- if (redraw && Focus())
- {
- ScrollSelectionIntoView(redraw);
- DoHighlightSelection(hlOff, GetCurrentHL());
- if (GetPrintHandler())
- DoDrawPrintFeedback(GetDrawableRect());
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::WriteToDeskScrap:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- void TTEView::WriteToDeskScrap() // override
- {
- FailOSErr(gClipboardMgr->PutDeskScrapData('TEXT', fText));
-
- if ((fStyleType == kWithStyle) && SpaceForStyles(0, SHRT_MAX))
- {
- short saveStart = (*fHTE)->selStart;
- short saveEnd = (*fHTE)->selEnd;
- SetSelect(0, SHRT_MAX, fHTE);
- Handle aHandle = (Handle)(TEGetStyleScrapHandle(fHTE));
- SetSelect(saveStart, saveEnd, fHTE);
- FailNIL(aHandle);
- FailOSErr(gClipboardMgr->PutDeskScrapData('styl', aHandle));
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SetActive:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
- void TTEView::SetActive(Boolean state)
- {
- CWhileOutlinePreferred setOP(fPreferOutline);
-
- if (fHTE && Focus()) // Try to focus because TEActivate/TEDeactivate may draw
- if (state)
- {
- if (fIdleFreq == kMaxIdleTime)
- SetIdleFreq(0); // Idle ASAP
- SetKeyScript(FontToScript(fTextStyle.tsFont));
- TEActivate(fHTE);
- fgCurrTEView = this; // So the global clickLoop routine can forward
- }
- else
- {
- TEDeactivate(fHTE);
- DoneTyping();
- fSpecsChanged = TRUE;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::SetEnable:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::SetEnable(Boolean state)
- {
- if (state && (fIdleFreq == kMaxIdleTime))
- SetIdleFreq(0); // Get correct idle set ASAP
-
- Inherited::SetEnable(state);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetEditText:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- TEditText* TTEView::GetEditText()
- {
- // TDialogTEView overrides this to return fEditText.
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::WordBounds:
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- Boolean TTEView::WordBounds(short charPos,
- short& wordStart,
- short& wordEnd)
- {
- wordStart = charPos;
- wordEnd = charPos;
-
- if ((charPos < 0) || (charPos > (*fHTE)->teLength - 1))
- return FALSE;
-
- // Start on a character boundary
- if (MACharacterByteType((*fText), charPos, smCurrentScript) == smFirstByte)
- charPos++;
-
- OffsetTable offs;
- FindWord((*fText), (*fHTE)->teLength, charPos, TRUE, NULL, offs);
- wordStart = (short)Min(offs[0].offFirst, offs[0].offSecond - 1);//!!! long->short
- wordEnd = (short)Max(offs[0].offFirst, offs[0].offSecond - 1);//!!! long->short
-
- return ((wordStart < wordEnd) || (MACharacterType((*fText), wordStart, smCurrentScript) % 8 != smCharPunct));
- }
-
- #if qDrag
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoMakeDragCursorRegion:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragRes
-
- RgnHandle TTEView::DoMakeDragCursorRegion()
- {
- RgnHandle dragCursorRegion = MakeNewRgn();
-
- if (HasDragManager())
- {
- CWhileOutlinePreferred setOP(fPreferOutline);
- TEGetHiliteRgn(dragCursorRegion, fHTE);
- }
-
- return dragCursorRegion;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::WillDrag:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragRes
-
- Boolean TTEView::WillDrag(const VPoint& localMouse,
- const RgnHandle dragCursorRegion)
- {
- return (!IsShiftKeyDown() && Inherited::WillDrag(localMouse, dragCursorRegion));
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetWillDragCursorID:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragRes
-
- short TTEView::GetWillDragCursorID()
- {
- return kNoResource;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::GetIsDraggingCursorID:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- short TTEView::GetIsDraggingCursorID()
- {
- return kNoResource;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoAddDragContent:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- void TTEView::DoAddDragContent()
- {
- TDragItem * dragItem = TDragDropSession::fgDragDropSession->AddDragItem(1);
-
- CFlavorFlags flags;
- dragItem->PromiseFlavor('TEXT', flags);
- StScrpHandle styleHandle = TEGetStyleScrapHandle(fHTE);
- if (styleHandle)
- {
- dragItem->PromiseFlavor('styl', flags);
- DisposeIfHandle((Handle)styleHandle);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::WillAcceptDrop:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- Boolean TTEView::WillAcceptDrop(CDragItemIterator& dragItemIterator)
- {
- if (TDragDropSession::fgDragDropSession->GetItemCount() != 1)
- return FALSE;
-
- TDragItem * firstItem = dragItemIterator.FirstDragItem();
- return firstItem->FlavorExists('TEXT');
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoDragEnter:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- void TTEView::DoDragEnter()
- {
- fLastCaretOffset = 0;
- fLastCaretTime = 0L;
- fCaretIsShown = FALSE;
-
- if (TDragDropSession::fgDragDropSession->GetCurrentDragSource() == this)
- {
- fHiliteRgn = MakeNewRgn();
- CWhileOutlinePreferred setOP(fPreferOutline);
- TEGetHiliteRgn(fHiliteRgn, fHTE);
- FailNIL(fHiliteRgn);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoDragWithin:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- void TTEView::DoDragWithin(const VPoint& localMouse)
- {
- unsigned long currentTime = TickCount();
- short offset = -1;
- Boolean caretMoved = TRUE;
-
- // if this view is the source and the target of the drag, don't allow dropping
- // into the selection
- if (fHiliteRgn && PtInRgn(ViewToQDPt(localMouse), fHiliteRgn))
- {
- if (fLastCaretOffset != -1)
- {
- offset = -1;
- caretMoved = TRUE; // force redraw
- }
- }
- else
- {
- offset = PtToOffset(localMouse);
- caretMoved = (fLastCaretOffset != offset);
- }
-
- if (caretMoved && fCaretIsShown)
- DrawCaret(fLastCaretOffset);
-
- if (caretMoved || (currentTime - fLastCaretTime) > GetCaretTime())
- {
- if (offset != -1)
- DrawCaret(offset);
- fLastCaretOffset = offset;
- fLastCaretTime = currentTime;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoDragLeave:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- void TTEView::DoDragLeave()
- {
- if (fCaretIsShown)
- DrawCaret(fLastCaretOffset);
-
- if (fHiliteRgn)
- fHiliteRgn = DisposeIfRgnHandle(fHiliteRgn);
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoMakeDragDropCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- TCommand* TTEView::DoMakeDragDropCommand(CommandNumber itsCommandNumber,
- CDragItemIterator& dragItemIterator)
- {
- TCommand * returnCommand = NULL;
-
- switch (itsCommandNumber)
- {
- case cDrag:
- {
- TTECommand * dragCommand = new TTECommand;
- dragCommand->ITECommand(this, itsCommandNumber, TRUE);
- returnCommand = dragCommand;
- }
- break;
-
- case cDrop:
- {
- Handle textHandle = NULL;
- StScrpHandle textStyles = NULL;
-
- TDragItem * firstItem = dragItemIterator.FirstDragItem();
- firstItem->FocusOnFlavor('TEXT');
- textHandle = firstItem->GetDataAsHandle();
- FailNIL(textHandle);
-
- if (firstItem->FlavorExists('styl'))
- {
- firstItem->FocusOnFlavor('styl');
- textStyles = (StScrpHandle)firstItem->GetDataAsHandle();
- }
-
- TTEDragDropCommand * dropCommand = new TTEDragDropCommand;
- dropCommand->ITEDragDropCommand(this, itsCommandNumber);
- dropCommand->SetNewText(textHandle, textStyles);
- dropCommand->SetNewStart(fLastCaretOffset);
- returnCommand = dropCommand;
- }
- break;
-
- case cDragMove:
- {
- if (fLastCaretOffset != -1) // can't drop into selection
- {
- TTEDragMoveCommand * dragMoveCommand = new TTEDragMoveCommand;
- dragMoveCommand->ITEDragMoveCommand(this, itsCommandNumber, fLastCaretOffset);
- returnCommand = dragMoveCommand;
- }
- }
- break;
-
- default:
- returnCommand = Inherited::DoMakeDragDropCommand(itsCommandNumber, dragItemIterator);
- break;
- }
- return returnCommand;
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DoFulfillPromise
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- void TTEView::DoFulfillPromise(TDragItem* promisedItem)
- {
- switch (promisedItem->GetFlavorType())
- {
- case 'TEXT':
- {
- MAVolatileInit(SignedByte, handleState, LockHandle(fText));
-
- FailInfo fi;
- Try(fi)
- {
- long byteCount = (*fHTE)->selEnd - (*fHTE)->selStart;
- char* textPtr = (char*) * fText + (*fHTE)->selStart;
- promisedItem->SetData(textPtr, byteCount);
- HSetState(fText, handleState);
- fi.Success();
- }
- else
- {
- HSetState(fText, handleState);
- fi.error = cantGetFlavorErr;
- fi.ReSignal();
- }
- }
- break;
-
- case 'styl':
- {
- MAVolatileInit(StScrpHandle, stylHandle, NULL);
-
- FailInfo fi;
- Try(fi)
- {
- stylHandle = TEGetStyleScrapHandle(fHTE);
- FailNIL(stylHandle);
-
- promisedItem->SetDataFromHandle((Handle)stylHandle);
- DisposeIfHandle((Handle)stylHandle);
- fi.Success();
- }
- else
- {
- DisposeIfHandle((Handle)stylHandle);
- fi.error = cantGetFlavorErr;
- fi.ReSignal();
- }
- }
- break;
-
- default:
- Inherited::DoFulfillPromise(promisedItem);
- break;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTEView::DrawCaret:
- //----------------------------------------------------------------------------------------
- #pragma segment MADragNonRes
-
- void TTEView::DrawCaret(short offset)
- {
- // Get the coordinates and the line of the offset to draw the caret.
- CPoint theLoc(TEGetPoint(offset, fHTE));
- short theLine = TEGetLine(offset, fHTE);
-
- short lineHeight = (short)TEGetHeight(theLine, theLine, fHTE);
-
- // For some reason, TextEdit doesn't return the proper coordinates
- // of the last offset in the field if the last character in the record
- // is a carriage return. TEGetPoint returns a point that is one line
- // higher than expected. The following code fixes this problem.
-
- if ((offset == (*fHTE)->teLength) && (*((*fHTE)->hText))[(*fHTE)->teLength - 1] == 0x0D)
- theLoc.v += lineHeight;
-
- PenNormal();
- // The caret supposed to be nice and dark (which the white pattern will accomplish)
- PenPat(&qd.white);
- PenMode(notPatXor); // set invert mode
-
- MoveToPt(theLoc - CPoint(1, 1)); // Draw the caret image
- Line(0, 1 - lineHeight);
-
- PenNormal(); // reset the pen
-
- fCaretIsShown = !fCaretIsShown;
- }
-
- #endif // qDrag
-
- //----------------------------------------------------------------------------------------
- // WriteChar:
- //----------------------------------------------------------------------------------------
- #if qDebug
- #pragma segment TEDebug
-
- const short kMaxIndex = 2047;
- typedef char x[kMaxIndex];
- typedef x* XPtr;
- typedef XPtr* XHandle;
-
-
- void TTEView::WriteChar(short index,
- Handle hText)
- {
- if (index <= kMaxIndex)
- fprintf(stderr, "[%1d:%1d]", index, *((XHandle)hText)[index]);
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // DumpTERecord:
- //----------------------------------------------------------------------------------------
- #if qDebug
- #pragma segment TEDebug
- void TTEView::DumpTERecord(TEHandle aTEH)
- {
- const short kMaxCharsToPrint = 100;
-
- short size;
-
- fprintf(stderr, "TE -- dest CRect: %s\n", (const char*)CRect((*aTEH)->destRect));
-
- size = (*aTEH)->teLength;
- fprintf(stderr, "Line ht: %1d", (*aTEH)->lineHeight);
- fprintf(stderr, "; teLength: %1d", size);
- fprintf(stderr, "; hText length: %1d", GetHandleSize((*aTEH)->hText));
- fprintf(stderr, "; lines: %1d", (*aTEH)->nLines);
- fprintf(stderr, "\n");
-
- if (gIntenseDebugging)
- {
- Handle hText = (*aTEH)->hText;
-
- fprintf(stderr, "Chars: ");
- long minSize = Min(2047, size - 1);
- for (short i = 0; i <= Min(kMaxCharsToPrint, minSize); ++i)
- {
- WriteChar(i, hText);
- if (i % 10 == 9)
- fprintf(stderr, "\n");
- }
- fprintf(stderr, "\n");
-
- fprintf(stderr, "InPort vis bbox: %s", (const char*)CRect((*TWindow::GetVisRegion((*aTEH)->inPort))->rgnBBox));
- fprintf(stderr, "; clip bbox: %s\n", (const char*)CRect((*TView::GetClipRegion((*aTEH)->inPort))->rgnBBox));
-
- if ((*aTEH)->inPort != qd.thePort)
- {
- fprintf(stderr, "thePort vis bbox: %s", (const char*)CRect((*TWindow::GetVisRegion(qd.thePort))->rgnBBox));
- fprintf(stderr, "; clip bbox: %s\n", (const char*)CRect((*TView::GetClipRegion(qd.thePort))->rgnBBox));
- }
- }
-
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // SetSelect: Usually, TTEView::SetSelection should be used instead. This
- // procedure is used by MacApp to set the selection 'behind the scenes' (that is,
- // in instances where an O.S. routine opperates on the current selection, but we
- // want to change a piece of the text without changing the selection).
- //----------------------------------------------------------------------------------------
- #pragma segment TERes
-
- void TTEView::SetSelect(short theStart,
- short theEnd,
- TEHandle hTE)
- {
- theStart = (short)Max(theStart, 0);
-
- (*hTE)->selStart = theStart;
- (*hTE)->selEnd = (short)MinMax(theStart, theEnd, (*hTE)->teLength);
- }
-
- //----------------------------------------------------------------------------------------
- // ClickLoopForTTEView:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- pascal Boolean TTEView::ClickLoopForTTEView()
- {
- return fgCurrTEView ? fgCurrTEView->ClickLoop() : TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // TEGetLine:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- short TTEView::TEGetLine(short offset,
- TEHandle theTE)
- // TEGetLine, given an offset and a TextEdit handle, returns the line number
- // of the line that contains the offset.
- {
- short returnVal;
-
- if (offset > (*theTE)->teLength)
- returnVal = (*theTE)->nLines;
- else
- {
- short line = 0;
- while ((*theTE)->lineStarts[line] < offset)
- line++;
-
- returnVal = line;
- }
-
- return returnVal;
- }
-
- //----------------------------------------------------------------------------------------
- // TEIsFrontOfLine:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- short TTEView::TEIsFrontOfLine(short offset,
- TEHandle theTE)
- // TEIsFrontOfLine, given an offset and a TextEdit handle, returns true if
- // the given offset is at the beginning of a line start.
- {
- short line = 0;
-
- if ((*theTE)->teLength == 0)
- return TRUE;
-
- if (offset >= (*theTE)->teLength)
- return ((*((*theTE)->hText))[(*theTE)->teLength - 1] == chReturn);
-
- while ((*theTE)->lineStarts[line] < offset)
- line++;
-
- return ((*theTE)->lineStarts[line] == offset);
- }
-
- //----------------------------------------------------------------------------------------
- // IsBlank:
- //----------------------------------------------------------------------------------------
- #pragma segment TENonRes
-
- Boolean TTEView::IsBlank(Handle theText,
- short offset,
- short& length)
- {
- Boolean returnVal = FALSE;
-
- short i = MACharacterType(*theText, offset, smCurrentScript);
- if ((i & smcClassMask) == smPunctBlank)
- {
- length = MACharacterByteType(*theText, offset, smCurrentScript) == smSingleByte ? 1 : 2;
-
- returnVal = TRUE;
- }
- else
- length = 0;
-
- return returnVal;
- }
-
-
- //----------------------------------------------------------------------------------------
- // End of UTEView.cp
-
- #pragma segment Inline
-
-